﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Text;

namespace StoragePeriod.COMP
{
    /// <summary>
    /// SendMail :给密码丢失人员发送密码到邮箱
    /// </summary>

    public class SendMail
    {
        private string _mailform;//发送方邮箱地址
        /// <summary>
        /// 发送方邮箱地址
        /// </summary>
        public string MailForm { get { return _mailform; } set { _mailform = value; } }
        private string _host;//发送方地址
        public string Host
        {
            set { _host = value; }
            get { return _host; }
        }
        private string _smtpUserName;//发送方用户名
        public string SmtpUserName
        {
            set { _smtpUserName = value; }
            get { return _smtpUserName; }
        }
        private string _smtpPassword;//发送方密码
        public string SmtpPassword
        {
            set { _smtpPassword = value; }
            get { return _smtpPassword; }
        }
        public SendMail()
        {
            _mailform= Config.GetAppSettings("mailFrom");
            _host = Config.GetAppSettings("host");
            _smtpUserName = Config.GetAppSettings("mailUserName");
            _smtpPassword = Config.GetAppSettings("mailPwd");
        }

        public bool Send(string MailTo, string Subject, string body)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(_mailform);
            if (mail == null) { return false; }
            mail.To.Add(new MailAddress(MailTo));
            mail.Subject = Subject;
            mail.Body = body;
            mail.IsBodyHtml = true;
            mail.BodyEncoding = Encoding.GetEncoding("GB2312");
            return Send(mail);
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        private bool Send(MailMessage mail)
        {
            SmtpClient sc = new SmtpClient();
            sc.UseDefaultCredentials = true;
            sc.EnableSsl = false;
            sc.Host = _host;
            sc.Credentials = new System.Net.NetworkCredential(_smtpUserName, _smtpPassword);
            try
            {
                sc.Send(mail);
                return true;
            }
            catch{
                return false;
                }
        }
    }
}
